﻿using System;

namespace DitaImport
{
	/// <summary>
	/// Passed as the parameter when the Log() event is fired, which is very frequently.
	/// This is the primary way for the DITA Import to communicate to whatever has initiated an import.
	/// </summary>
	/// <remarks>
	/// Use to display import progress in the user interface or record progress in a log file
	/// </remarks>
	public class LogEventArgs : EventArgs
	{
		#region Fields (3) 

		/// <summary>
		/// Exception for the log event
		/// </summary>
		public Exception Exception;
		/// <summary>
		/// Message for the log event
		/// </summary>
		public string Message;
		/// <summary>
		/// Severity of the log event
		/// </summary>
		public Importer.Severity Severity;

		#endregion Fields 

		#region Constructors (2) 

		internal LogEventArgs(Importer.Severity Severity, string Message, Exception Exception)
			: base()
		{
			this.Severity = Severity;
			this.Message = Message;
			this.Exception = Exception;
		}

		internal LogEventArgs(Importer.Severity Severity, string Message)
			: base()
		{
			this.Severity = Severity;
			this.Message = Message;
		}

		#endregion Constructors 

		#region Methods (2) 

		// Public Methods (1) 

		/// <summary>
		/// Returns a string representation of this <see cref="LogEventArgs"/>
		/// </summary>
		/// <returns>String representation of this object</returns>
		public override string ToString()
		{
			switch (Severity)
			{
				case Importer.Severity.Information:
					return "        " + Message + RecursiveExceptionMessage(Exception);
				case Importer.Severity.Warning:
					return "Warning " + Message + RecursiveExceptionMessage(Exception);
				case Importer.Severity.Error:
					return "ERROR!  " + Message + RecursiveExceptionMessage(Exception);
				default:
					return "??????? " + Message + RecursiveExceptionMessage(Exception);
			}
		}
		// Private Methods (1) 

		private string RecursiveExceptionMessage(Exception Exception)
		{
			//If Exception Is Nothing Then
			//    Return String.Empty
			//Elseif...
			if (Exception.InnerException == null)
			{
				return Exception.Message;
			}
			else
			{
				return Exception.Message + Environment.NewLine + RecursiveExceptionMessage(Exception.InnerException);
			}
		}

		#endregion Methods 
	}
}